<template>
  <div id="app" class="wrapper wrapper-content animated fadeInRight v-cloak" v-cloak>
    <div class="row">
      <div class="ibox float-e-margins">
        <div class="ibox-content col-md-12">
          <form id="orgInfoForm" method="post" class="form m-t" @submit.prevent="save()" accept-charset="UTF-8">
            <input type="hidden" name="id" v-model="item.id"/>
            <input type="hidden" name="token" v-model="item.token"/>
            <input type="hidden" name="newFlag" v-model="newFlag"/>
            <div class="clearfix "></div>
            <div class="col-md-6">

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">部门类型：</label>
                <div class="col-md-12">
                  <select data-placeholder="选择部门类型" id="bean-type" name="type" class="form-control" required="" aria-required="true" aria-describedby="bean-type-error" aria-invalid="true">
                    <option value="">--请选择部门类型--</option>
                    <option value="-1" v-if="staff.dto().accountType==-1" :selected="item.type==-1">河长办</option>
                    <option value="0" v-if="staff.dto().accountType>-1" :selected="item.type==0">业务部门</option>
                    <option value="1" v-if="staff.dto().accountType>-1" :selected="item.type==1">联系部门</option>
                  </select>
                  <span id="bean-type-error" class="help-block m-b-none" for="bean-type"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">名称：</label>
                <div class="col-md-12">
                  <input id="bean-name" name="name" v-model="item.name" class="form-control" type="text" placeholder="请输入名称" required="" aria-required="true" aria-describedby="bean-name-error" maxlength="100" aria-invalid="true">
                  <span id="bean-name-error" class="help-block m-b-none" for="bean-name"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">上级部门：</label>
                <div class="col-md-12">
                  <select data-placeholder="选择上级部门" id="bean-parentId" name="parentId" class="form-control" aria-describedby="bean-parentId-error" aria-invalid="true">
                    <option value="0">--根节点--</option>
                    <option :value="orgItem.id" :selected="item.parentId == orgItem.id" v-for="orgItem in orgItems">{{ orgItem.name }}</option>
                  </select>
                  <span id="bean-parentId-error" class="help-block m-b-none" for="bean-parentId"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">机构职责：</label>
                <div class="col-md-12">
                  <input id="bean-duty" name="duty" v-model="item.duty" class="form-control" type="text" placeholder="请输入机构职责" aria-describedby="bean-duty-error" aria-invalid="true">
                  <span id="bean-duty-error" class="help-block m-b-none" for="bean-duty"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">单位地址：</label>
                <div class="col-md-12">
                  <input id="bean-address" name="address" v-model="item.address" class="form-control" type="text" placeholder="请输入单位地址" aria-describedby="bean-address-error" maxlength="255" aria-invalid="true">
                  <span id="bean-address-error" class="help-block m-b-none" for="bean-address"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">单位电话：</label>
                <div class="col-md-12">
                  <input id="bean-telphone" name="telphone" v-model="item.telphone" class="form-control" type="text" placeholder="请输入单位电话" aria-describedby="bean-telphone-error" maxlength="25" aria-invalid="true">
                  <span id="bean-telphone-error" class="help-block m-b-none" for="bean-telphone"></span>
                </div>
              </div>

            </div>
            <div class="col-md-6">

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">负责人：</label>
                <div class="col-md-12">
                  <input id="bean-leadingCadre" name="leadingCadre" v-model="item.leadingCadre" class="form-control" type="text" placeholder="请输入负责人" aria-describedby="bean-leadingCadre-error" maxlength="85" aria-invalid="true">
                  <span id="bean-leadingCadre-error" class="help-block m-b-none" for="bean-leadingCadre"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">负责人电话：</label>
                <div class="col-md-12">
                  <input id="bean-leadingCadreCallphone" name="leadingCadreCallphone" v-model="item.leadingCadreCallphone" class="form-control" type="text" placeholder="请输入负责人电话" aria-describedby="bean-leadingCadreCallphone-error" maxlength="255" aria-invalid="true">
                  <span id="bean-leadingCadreCallphone-error" class="help-block m-b-none" for="bean-leadingCadreCallphone"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">联系人：</label>
                <div class="col-md-12">
                  <input id="bean-contacts" name="contacts" v-model="item.contacts" class="form-control" type="text" placeholder="请输入联系人" aria-describedby="bean-contacts-error" maxlength="25" aria-invalid="true">
                  <span id="bean-contacts-error" class="help-block m-b-none" for="bean-contacts"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">联系人电话：</label>
                <div class="col-md-12">
                  <input id="bean-contactsCallphone" name="contactsCallphone" v-model="item.contactsCallphone" class="form-control" type="text" placeholder="请输入联系人电话" aria-describedby="bean-contactsCallphone-error" maxlength="25" aria-invalid="true">
                  <span id="bean-contactsCallphone-error" class="help-block m-b-none" for="bean-contactsCallphone"></span>
                </div>
              </div>

              <div class="form-group clearfix">
                <label class="col-md-8 control-label">机构人员数量：</label>
                <div class="col-md-12">
                  <input id="bean-totalQuantity" name="totalQuantity" v-model="item.totalQuantity" class="form-control" type="text" placeholder="请输入机构人员数量" aria-describedby="bean-totalQuantity-error" aria-invalid="true">
                  <span id="bean-totalQuantity-error" class="help-block m-b-none" for="bean-totalQuantity"></span>
                </div>
              </div>
            </div>
            <div class="clearfix "></div>
            <div class="col-md-12">
              <vue-area
                  areaId="areaId"
                  areaName="areaName"
                  :areaIdVal="item.areaId||staff.dto().areaId"
                  :areaNameVal="item.areaName||staff.dto().areaName"
                  :areaFixed="item.areaId||staff.dto().areaId"
                  :selectBy="item.areaId||staff.dto().areaId"
                  areaRequired="true,true,false,false,false"
              ></vue-area>
            </div>
            <div class="clearfix "></div>
            <div class="col-12 text-center" style="height: 50px;">
              <div>
                <input class="btn btn-primary" type="submit" value="保存"/> | <a @click="lbox.closeMyBoxLayer()" class="btn btn-danger" href="javascript:">返回</a>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>
<script type="text/javascript">
import '@/assets/css/style.css'

import $ from '@/assets/js/jquery-vendor.js'
import 'jquery.cookie'
import axios from 'axios'
import '@/assets/js/validate/validation-vendor.js'

import {apiUtil, axiosContentType, site} from '@/assets/js/boss'

import vueArea from '@/components/vue-area'

export default {
  components: {
    'vue-area': vueArea
  },
  data() {
    return {
      item: {},
      newFlag: 0,
      uuidToken: '',
      orgItems: []
    }
  },
  mounted() {
    let that = window.$vueApp = this;
    document.getElementsByTagName('body')[0].className = 'fixed-sidebar full-height-layout';
    $('#orgInfoForm').validate();
    that.staff.init(function () {
      that.newFlag = sessionStorage.getItem(site.orgInfo.info);
      if (that.newFlag) that.uuidToken = apiUtil.guid();
      if (apiUtil.existSessionKey(site.orgInfo.info)) {
        that.info();
      }
      that.getOrglist();
    });
  },
  methods: {
    info() {
      let that = this;
      axios.get(site.orgInfo.info + sessionStorage.getItem(site.orgInfo.info), {}).then(function (response) {
        const result = response.data;
        if (result.code === 0) {
          that.item = result.data;
        } else {
          alert(result.message);
        }
      });
    },
    getOrglist() {
      let that = this;
      axios.post(site.orgInfo.list + '?crtOrgId=' + sessionStorage.getItem(site.orgInfo.info), {}).then(function (response) {
        const result = response.data;
        if (result.code === 0) {
          that.orgItems = result.data;
        } else {
          alert(result.message);
        }
      });
    },
    save() {
      let that = this;
      if ($('#orgInfoForm').valid()) {
        axios.post(site.orgInfo.save, $('#orgInfoForm').serialize(),
            axiosContentType.xWwwFormUrlencoded).then(function (response) {
          const result = response.data;
          // console.info("获取数据.." + JSON.stringify(result));
          if (result.code === 0) {
            parent.$vueApp.page();
            alert('保存成功!');
            that.lbox.closeMyBoxLayer()
          } else {
            alert(result.message);
          }
        });
      }
    }
  }
}
</script>

